<!-- 员工管理 -->
<template>
  <el-form class="query-container" ref="queryFormRef" :model="queryParams" :inline="true" @submit.native.prevent>
    <el-form-item label="" prop="field" style="margin-right:5px;">
      <el-select v-model="queryParams.field" style="width:150px;">
        <el-option value="u.name" label="姓名"></el-option>
        <el-option value="s.id" label="员工编号"></el-option>
        <el-option value="phone" label="手机号"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="" prop="keyword">
      <el-input
          v-model="queryParams.keyword"
          clearable
          placeholder="输入关键字查询"
          @keyup.enter.native="handleQuery"
          style="width:150px;"
      />
    </el-form-item>
    <el-form-item>
      <el-button v-if="$hasPermission('crm:personnel:staff:select')"
                 type="primary" @click="handleQuery">搜索</el-button>
      <el-button @click="resetQuery">重置</el-button>
    </el-form-item>
    <br>
    <el-row :gutter="10" style="margin-bottom:8px">
      <el-col :span="1.5">
        <el-button v-if="$hasPermission('crm:personnel:staff:add')"
                   type="primary" plain @click="$router.push('/crm/staff/staff_add')">新增</el-button>
      </el-col>
    </el-row>
  </el-form>

  <div class="inner-container">
    <el-table v-loading="loading" :data="tableData" @selection-change="handleSelectionChange">
      <el-table-column prop="createTime" label="创建时间"></el-table-column>
      <el-table-column prop="id" label="员工编号"></el-table-column>
      <el-table-column prop="userName" label="姓名"></el-table-column>
      <el-table-column prop="deptName" label="所属部门"></el-table-column>
      <el-table-column prop="roleName" label="所属角色"></el-table-column>
      <el-table-column prop="phone" label="登录账号" ></el-table-column>
      <el-table-column prop="email" label="邮箱绑定"></el-table-column>
      <el-table-column prop="password" label="登录密码"
                       :formatter="passwordFormatter"></el-table-column>
      <el-table-column prop="status" label="账号状态"
                       :formatter="userStatusFormatter"></el-table-column>
      <el-table-column label="操作" align="center" fixed="right" width="160">
        <template #default="scope">
          <el-button v-if="$hasPermission('crm:personnel:staff:updata')" type="primary" link
                     @click="goToUpdateView(scope.row)">编辑</el-button>
          <el-button v-if="$hasPermission('crm:personnel:staff:del')"type="primary" link
                     @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页栏 -->
    <br>
    <el-pagination
        :total="total"
        v-model:current-page="queryParams.pageNum"
        v-model:page-size="queryParams.pageSize"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
    />
  </div>
</template>

<script setup>
import {onMounted, ref} from 'vue'
import {ElMessage, ElMessageBox} from 'element-plus'
import request from "@/utils/request";
import {useRouter} from "vue-router";



const router = useRouter()

//定义加载方法
const loading = ref(false)
//引用
const queryFormRef = ref()
// 定义查询参数
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  id: undefined,
  userId: undefined,
  deptId: undefined,
  roleId: undefined,
  name: undefined,
  phone: undefined,
  dept: undefined,
  role: undefined,
  email: undefined,
  status: undefined,
  field: undefined,
  keyword: undefined,
})
//定义页码数
const total = ref(0)
//定义数组用来保存表格数据
const tableData = ref([])
const selectedIds = ref([])


//测试数据
const listData = () => {
  loading.value = true
  request.get('/api/crm/personnel/staff/select', {
    params: queryParams.value
  }).then(res => {
    total.value = res.data.total
    tableData.value = res.data.list
    loading.value = false
  })
}

//处理选择更改
const handleSelectionChange = (selection) => {
  selectedIds.value = selection.map(item => item.id)
}

//处理搜索
const handleQuery = () => {
  queryParams.value.pageNum = 1
  listData()
}

//重置搜索条件
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

//定义删除的方法
const handleDelete = (row) => {
  let ids = row ? [row.id] : [...selectedIds.value]
  if (ids.length === 0) {
    ElMessage.error('请选择要删除的记录')
    return
  }
  ElMessageBox.confirm('确定删除吗？', '提示', {
    type: 'warning'
  }).then(() => {
    request.delete(`/api/crm/personnel/staff/delete?ids=`+ids)
        .then(res => {
        selectedIds.value.length = 0
        listData()
        ElMessage.success('删除成功')
    })
  }).catch(() => {})
}

//跳转到编辑页面
const goToUpdateView = (row) => {
  let id = row.id || selectedIds.value
  router.push({path: '/crm/staff/staff_update', query: {id: id}})

}

//处理页码数变化
const handleCurrentChange = (val) => {
  queryParams.value.pageNum = val
  listData()
}

//处理每页条数变化
const handleSizeChange = (val) => {
  queryParams.value.pageSize = val
  listData()
}

//定义处理账号状态表格列的显示文字转换
const userStatusFormatter = (row, column, cellValue, index)=>{
  if (cellValue == 0){
    cellValue = '正常';
  }else if (cellValue == 1){
    cellValue = '停用';
  }
  return cellValue;
}


// 添加格式化方法
const passwordFormatter = (row, column, cellValue) => {
  return cellValue ? '******' : '';
}


//一进页面就获取数据
onMounted(() => {
  listData()
})
</script>

<style scoped>

</style>
